/**
 * This file is part of Wasagent.
 *
 * Wasagent is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wasagent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wasagent. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package net.wait4it.wasagent.tests;

import com.ibm.websphere.pmi.stat.WSBoundedRangeStatistic;
import com.ibm.websphere.pmi.stat.WSCountStatistic;
import com.ibm.websphere.pmi.stat.WSRangeStatistic;
import com.ibm.websphere.pmi.stat.WSStats;

/**
 * @author Yann Lambret
 *
 */
public abstract class TestUtils {

	public static int checkResult(long n1, long n2, long critical, long warning, int code) {
		if (isCritical(n1, n2, critical)) {
			code = 2;
		} else if (isWarning(n1, n2, warning) && code == 0) {
			code = 1;
		}
		return code;
	}

	public static int checkResult(long n, long critical, long warning, int code) {
		if (isCritical(n, critical)) {
			code = 2;
		} else if (isWarning(n, warning) && code == 0) {
			code = 1;
		}
		return code;
	}

	public static WSBoundedRangeStatistic getBoundedRangeStats(WSStats stats, int stat) {
		return (WSBoundedRangeStatistic) stats.getStatistic(stat);
	}

	public static WSCountStatistic getCountStats(WSStats stats, int stat) {
		return (WSCountStatistic) stats.getStatistic (stat);
	}

	public static WSRangeStatistic getRangeStats(WSStats stats, int stat) {
		return (WSRangeStatistic) stats.getStatistic(stat);
	}

	public static long format(long value) {
		return (value/1024);
	}

	private static boolean isCritical(long n1, long n2, long critical) {
		long ratio = ratio(n1, n2);
		return ratio >= critical;
	}

	private static boolean isCritical(long n, long critical) {
		return n >= critical;
	}

	private static boolean isWarning(long n1, long n2, long warning) {
		long ratio = ratio(n1, n2);
		return ratio >= warning;
	}

	private static boolean isWarning(long n, long warning) {
		return n >= warning;
	}

	private static long ratio(long n1, long n2) {
		return Math.round((double) n1 / (double) n2 * 100);

	}

}
